package cn.loioi.web.controller.admin.masseage;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.loioi.multi.enums.MessageState;
import cn.loioi.multi.enums.MessageType;
import cn.loioi.multi.mysql.model.SysMessage;
import cn.loioi.multi.mysql.model.SysUser;
import cn.loioi.multi.service.SysMessageService;
import cn.loioi.multi.service.user.SysUserService;
import cn.loioi.util.RequestUtil;
import cn.loioi.util.page.PageUtil;
import cn.loioi.web.common.BaseUserController;

/**
 * <br>
 * (c) Copyright 2016 by <br>
 * @description :新建消息 <br>
 * @file_name :NewMessageContorller.java <br>
 * @system_name :multi_project_web <br>
 * @author :yyzh <br>
 * @创建时间 :2016年7月26日 下午1:42:22
 */
@Controller
@RequestMapping("/admin/masseage/send")
public class SendMessageContorller extends BaseUserController {
	private @Resource SysUserService sysUserService;
	private @Resource SysMessageService sysMessageService;

	public SendMessageContorller() {
		super("admin/masseage/send/", "message:send");
	}

	@RequestMapping("index.jspx")
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("types", MessageType.values());
		return returnUrl("index", model);
	}

	/**
	 * 根据用户名查找用户列表
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getUser.json")
	public Model getUser(HttpServletRequest request, Model model) {
		String name = RequestUtil.getValueStr(request, "term");
		Integer pageNo = RequestUtil.getValueInt(request, "pageNo", 1);
		sysUserService.setPage(new PageUtil(pageNo, 10));
		List<SysUser> list = sysUserService.selectByName(name);
		List<Map<String, Object>> options = new ArrayList<>();
		for (SysUser sysUser : list) {
			Map<String, Object> map = new HashMap<>();
			map.put("id", sysUser.getAccount());
			map.put("text", sysUser.getName());
			options.add(map);
		}
		model.addAttribute("results", options);
		return model;
	}

	/**
	 * 发送消息
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("send.json")
	public Model send(HttpServletRequest request, Model model) {
		String[] accounts = RequestUtil.getValue(request, "account");
		SysMessage message = new SysMessage();
		message.setSend(getAccount());
		message.setState(MessageState.UN_READ.getCode());
		message.setTitle(RequestUtil.getValueStr(request, "title"));
		message.setType(RequestUtil.getValueInt(request, "type"));
		message.setSendTime(RequestUtil.getValueDate(request, "sendTime", "yyyy/MM/dd HH:mm:ss", null));
		message.setLink(RequestUtil.getValueStr(request, "link"));
		message.setContent(RequestUtil.getValueStr(request, "content"));
		message.setParam(RequestUtil.getValueStr(request, "param"));
		message.setCreateTime(new Date());
		for (String string : accounts) {
			message.setAccount(string);
			message.setId(null);
			sysMessageService.insert(message);
		}
		return renderSuccess("", model);
	}
}
